package meta.api.web.account.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import meta.api.web.account.businessinfo.UserInfo;
import meta.api.web.account.dao.UserMngDao;
import meta.common.util.ListUtil;

@Component
public class UserMngService {

    @Autowired
    private UserMngDao userMngDao;

    public boolean checkExistAccount(String account) {
        return userMngDao.checkExistAccount(account);
    }

    public boolean checkExistAccountId(String id) {
        return userMngDao.checkExistAccountId(id);
    }

    public boolean checkAccountUsedByOther(String id, String account) {
        return userMngDao.checkAccountUsedByOther(id, account);
    }

    public int getUserTotal(String query) {
        return userMngDao.getUserTotal(query);
    }

    public List<UserInfo> getUserList(String query, Integer current, Integer pageSize) {
        return userMngDao.getUserList(query, current, pageSize);
    }

    public void registerUser(UserInfo user) {
        saveRole(user.getRoleNmList());
        userMngDao.registerUser(user);
    }

    public UserInfo getUser(String account, boolean pswdFlg) {
        return userMngDao.getUser(account, pswdFlg);
    }

    public void updateUser(UserInfo user) {
        saveRole(user.getRoleNmList());
        userMngDao.updateUser(user);
    }

    public void deleteUser(String id) {
        userMngDao.deleteUser(id);
    }

    public boolean checkLogin(String account, String password) {
        return userMngDao.checkLogin(account, password);
    }

    public void updatePassword(String account, String password) {
        userMngDao.updatePassword(account, password);
    }

    public void updateEmail(UserInfo user) {
        userMngDao.updateEmail(user);
    }

    private void saveRole(List<String> roleNmList) {
        if (ListUtil.isEmptyList(roleNmList)) {
            return;
        }
        for (String roleNm : roleNmList) {
            if (!userMngDao.checkExistRole(roleNm)) {
                userMngDao.insertRole(roleNm);
            }
        }
    }
}
